package cn.zhku.edu.userservice.service.impl;

import cn.zhku.edu.feign.common.R;
import cn.zhku.edu.feign.pojo.entry.Product;
import cn.zhku.edu.userservice.mapper.UserMapper;
import cn.zhku.edu.feign.pojo.entry.User;
import cn.zhku.edu.userservice.service.UserServies;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author clk
 * @Date 2021/9/13 17:03
 */

@Service
public class UserServieslmpl extends ServiceImpl<UserMapper, User> implements UserServies {
    @Autowired
    UserMapper userMapper;


    @Override
    public int deleteByPrimaryKey(String uid) {
        return userMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(String uid) {
        return userMapper.selectByPrimaryKey(uid);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public User findByName(String name) {
        return userMapper.findByName(name);
    }

    @Override
    public String findPswByName(String UserName) {
        return userMapper.findPswByName(UserName);
    }

    @Override
    public R login(User user) {
        try {
            User userExistN = userMapper.findByName(user.getUsername());
            if (userExistN != null){
                String userExistP=userMapper.findPswByName(user.getUsername());
                if (userExistP.equals(user.getPassword())){
                    return new R(200,"1",userExistN);
                }else {
                    return new R(200,"0");
                }
            }else {
                return new R(200,"-1");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new R(200,"访问成功！",e.getMessage());
        }
    }

    @Override
    public String regist(User user) {
        try{
            User userExist = userMapper.findByName(user.getUsername());
            if (user.getUsername().equals("")){
                return "账户名不能为空";
            }else if (user.getPassword().equals("")){
                return "密码不能为空";
            }else if (userExist != null){
                return "账户已经存在";
            }else {
                userMapper.insertSelective(user);
                return "注册成功";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

}
